package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.SortBean;
import com.gongpingjialoan.utility.BaseViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2015/9/29.
 */
public class SortAdapter extends BaseAdapter {

    private List<SortBean> sortBeans;
    private Context context;
    private int position = -1;
    private boolean isCutPrice = false;
    private boolean isGoodcar = false;


    public SortAdapter(List<SortBean> sortBeans, Context context) {
        this.sortBeans = sortBeans;
        this.context = context;
    }


    public void setIsCutPrice(boolean isCutPrice) {
        this.isCutPrice = isCutPrice;
        notifyDataSetChanged();
    }

    public void setIsGoodcar(boolean isGoodcar) {
        this.isGoodcar = isGoodcar;
        notifyDataSetChanged();
    }


    public void setPosition(int i) {
        position = i;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return sortBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return sortBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.sort_item, null, false);
        }
        TextView tv = BaseViewHolder.get(convertView, R.id.title);
        ImageView image = BaseViewHolder.get(convertView, R.id.image);
        tv.setText(sortBeans.get(i).getTitle());


        if ("认证好车".equals(sortBeans.get(i).getTitle())) {
            if (isGoodcar) {
                tv.setTextColor(context.getResources().getColor(R.color.nav_bg_color));
            } else {
                tv.setTextColor(context.getResources().getColor(R.color.discount_title));
            }
            image.setVisibility(View.VISIBLE);
            image.setImageResource(R.drawable.zheng_img);

        } else {
            image.setVisibility(View.GONE);
            if (i != position) {
                tv.setTextColor(context.getResources().getColor(R.color.discount_title));
            } else {
                tv.setTextColor(context.getResources().getColor(R.color.nav_bg_color));
            }
        }

        if ("降价好车".equals(sortBeans.get(i).getTitle())) {
            if (isCutPrice) {
                tv.setTextColor(context.getResources().getColor(R.color.nav_bg_color));
            } else {
                tv.setTextColor(context.getResources().getColor(R.color.discount_title));
            }
            image.setVisibility(View.VISIBLE);
            image.setImageResource(R.drawable.sort_jiang_img);
        } else {
            if (!"认证好车".equals(sortBeans.get(i).getTitle())) {
                image.setVisibility(View.GONE);
            }
        }


        return convertView;
    }
}
